package com.wis.tools;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * 这个循环是包开始和包结束的数据
 *
 * @Description: traffic_wis
 * @author: z1042
 * @Date: Created in 2018/12/13 20:50
 * @Modified By:
 */
public class RiskUtiles {

    public static String foreach(String[] strings, Integer start, Integer end) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = start; i < end; i++) {
            stringBuffer.append(strings[i] + ",");
        }
        return stringBuffer.append(strings[end]).toString();

    }

    /**
     * 雨夹雪的卡值
     *
     * @param jsonKaZhi
     * @param common
     * @param nengjiandu
     * @return
     * @description 通用的卡值方法
     */
    public String sleetKaZhi(JSONObject jsonKaZhi, String nengjiandu) {
        float vbValue = Float.parseFloat(nengjiandu);
        if (vbValue < 0) {
            return "2";
        } else if (StringUtils.isNotBlank(jsonKaZhi.getString("threshold14")) && vbValue < Float.parseFloat(jsonKaZhi.getString("threshold14"))) {
            return "4";
        } else if (StringUtils.isNotBlank(jsonKaZhi.getString("threshold13")) && vbValue < Float.parseFloat(jsonKaZhi.getString("threshold13"))) {
            return "3";
        } else {
            return "2";
        }
    }


    /**
     * @param jsonKaZhi
     * @param common
     * @param nengjiandu
     * @return
     * @description 通用的卡值方法
     */
    public String commonKaZhi(JSONObject jsonKaZhi, String common, String nengjiandu) {
        float commonValue = Float.parseFloat(common);
        float vbValue = Float.parseFloat(nengjiandu);
        if (commonValue == -999.9f) {
            return "-1";
        } else if (StringUtils.isNotBlank(jsonKaZhi.getString("threshold14")) && commonValue >= Float.parseFloat(jsonKaZhi.getString("threshold14"))) {
            return "4";
        } else if (StringUtils.isNotBlank(jsonKaZhi.getString("threshold13")) && commonValue >= Float.parseFloat(jsonKaZhi.getString("threshold13"))) {
            if (vbValue != -999.9f && StringUtils.isNotBlank(jsonKaZhi.getString("threshold24")) && vbValue < Float.parseFloat(jsonKaZhi.getString("threshold24"))) {
                return "4";
            } else {
                return "3";
            }
        } else if (StringUtils.isNotBlank(jsonKaZhi.getString("threshold12")) && commonValue >= Float.parseFloat(jsonKaZhi.getString("threshold12"))) {
            if (vbValue != -999.9f && StringUtils.isNotBlank(jsonKaZhi.getString("threshold23")) && vbValue < Float.parseFloat(jsonKaZhi.getString("threshold23"))) {
                return "4";
            } else if (vbValue != -999.9f && StringUtils.isNotBlank(jsonKaZhi.getString("threshold22")) && vbValue < Float.parseFloat(jsonKaZhi.getString("threshold22"))) {
                return "3";
            } else {
                return "2";
            }
        } else if (StringUtils.isNotBlank(jsonKaZhi.getString("threshold11")) && commonValue >= Float.parseFloat(jsonKaZhi.getString("threshold11"))) {
            if (vbValue != -999.9f && StringUtils.isNotBlank(jsonKaZhi.getString("threshold24")) && vbValue < Float.parseFloat(jsonKaZhi.getString("threshold24"))) {
                return "4";
            } else if (vbValue != -999.9f && StringUtils.isNotBlank(jsonKaZhi.getString("threshold23")) && vbValue < Float.parseFloat(jsonKaZhi.getString("threshold23"))) {
                return "3";
            } else if (vbValue != -999.9f && StringUtils.isNotBlank(jsonKaZhi.getString("threshold22")) && vbValue < Float.parseFloat(jsonKaZhi.getString("threshold22"))) {
                return "2";
            } else {
                return "1";
            }
        } else {
            return "0";
        }
    }

    /**
     * @param jsonKaZhi
     * @param common
     * @param nengjiandu
     * @return
     * @description 气温的卡值方法：省去了能见度的判断
     */
    public String qiWenKaZhi(JSONObject jsonKaZhi, String common) {
        float commonValue = Float.parseFloat(common);
        if (commonValue == -999.9f) {
            return "-1"; // 异常值处理为5
        } else if (StringUtils.isNotBlank(jsonKaZhi.getString("threshold14")) && commonValue >= Float.parseFloat(jsonKaZhi.getString("threshold14"))) {
            return "4";
        } else if (StringUtils.isNotBlank(jsonKaZhi.getString("threshold13")) && commonValue >= Float.parseFloat(jsonKaZhi.getString("threshold13"))) {
            return "3";
        } else if (StringUtils.isNotBlank(jsonKaZhi.getString("threshold12")) && commonValue >= Float.parseFloat(jsonKaZhi.getString("threshold12"))) {
            return "2";
        } else if (StringUtils.isNotBlank(jsonKaZhi.getString("threshold11")) && commonValue >= Float.parseFloat(jsonKaZhi.getString("threshold11"))) {
            return "1";
        } else {
            return "0";
        }
    }

    public String PPKaZhi_300(JSONObject jsonKaZhi, String jiangshuiliang, String jiangshuixiangtai) {
        String[] jsl = jiangshuiliang.split(",", -1);
        String[] jsxt = jiangshuixiangtai.split(",", -1);

        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < jsl.length; i++) {
            float ppValue = Float.parseFloat(jsl[i]);
            float phValue = Float.parseFloat(jsxt[i]);
            // 不下雨
            if (ppValue == -999 || phValue == -999) {
                stringBuffer.append("0");
            } else if (phValue == 300) {
                stringBuffer.append("0");
                // 下雨
            }
            stringBuffer.append(",");
        }
        return stringBuffer.substring(0, stringBuffer.length() - 1);
    }

    public String VBKaZhi(JSONObject jsonKaZhi, String njd) {
        float vbValue = Float.parseFloat(njd);
        if (vbValue < 0) {
            return "-1";
        } else if (StringUtils.isNotBlank(jsonKaZhi.getString("threshold14")) && vbValue < Float.parseFloat(jsonKaZhi.getString("threshold14"))) {
            return "4";
        } else if (StringUtils.isNotBlank(jsonKaZhi.getString("threshold13")) && vbValue < Float.parseFloat(jsonKaZhi.getString("threshold13"))) {
            return "3";
        } else if (StringUtils.isNotBlank(jsonKaZhi.getString("threshold12")) && vbValue < Float.parseFloat(jsonKaZhi.getString("threshold12"))) {
            return "2";
        } else if (StringUtils.isNotBlank(jsonKaZhi.getString("threshold11")) && vbValue < Float.parseFloat(jsonKaZhi.getString("threshold11"))) {
            return "1";
        } else {
            return "0";
        }
    }

    public String WDKaZhi(JSONObject jsonKaZhi, String fengsu, String nengjiandu) {
        String[] fs = fengsu.split(",", -1);
        String[] njd = nengjiandu.split(",", -1);
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < fs.length; i++) {
            float wsValue = Float.parseFloat(fs[i]);
            float vbValue = Float.parseFloat(njd[i]);
            if (wsValue == -999 || vbValue == -999) {
                stringBuffer.append("0");
            } else if (wsValue >= Float.parseFloat(jsonKaZhi.getString("33410"))) {
                stringBuffer.append("4");
            } else if (wsValue >= Float.parseFloat(jsonKaZhi.getString("33310"))) {
                if (vbValue < Float.parseFloat(jsonKaZhi.getString("32410"))) {
                    stringBuffer.append("4");
                } else {
                    stringBuffer.append("3");
                }
            } else if (wsValue >= Float.parseFloat(jsonKaZhi.getString("33210"))) {
                if (vbValue < Float.parseFloat(jsonKaZhi.getString("32310"))) {
                    stringBuffer.append("4");
                } else if (vbValue < Float.parseFloat(jsonKaZhi.getString("32210"))) {
                    stringBuffer.append("3");
                } else {
                    stringBuffer.append("2");
                }
            } else if (wsValue >= Float.parseFloat(jsonKaZhi.getString("33110"))) {
                if (vbValue < Float.parseFloat(jsonKaZhi.getString("32410"))) {
                    stringBuffer.append("4");
                } else if (vbValue < Float.parseFloat(jsonKaZhi.getString("32310"))) {
                    stringBuffer.append("3");
                } else if (vbValue < Float.parseFloat(jsonKaZhi.getString("32210"))) {
                    stringBuffer.append("2");
                } else {
                    stringBuffer.append("1");
                }
            } else {
                stringBuffer.append("0");
            }
            stringBuffer.append(",");
        }
        return stringBuffer.substring(0, stringBuffer.length() - 1);
    }

    public String PPKaZhi(JSONObject jsonKaZhi, String jiangshuiliang, String nengjiandu, String jiangshuixiangtai) {
        String[] jsl = jiangshuiliang.split(",", -1);
        String[] njd = nengjiandu.split(",", -1);
        String[] jsxt = jiangshuixiangtai.split(",", -1);

        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < jsl.length; i++) {
            float ppValue = Float.parseFloat(jsl[i]);
            float vbValue = Float.parseFloat(njd[i]);
            float phValue = Float.parseFloat(jsxt[i]);
            // 不下雨
            if (ppValue == -999 || vbValue == -999 || phValue == -999) {
                stringBuffer.append("0");
            } else if (phValue == 300) {
                stringBuffer.append("0");
                // 下雨
            } else if (phValue == 301) {
                if (ppValue >= Float.parseFloat(jsonKaZhi.getString("11410"))) {
                    stringBuffer.append("4");
                } else if (ppValue >= Float.parseFloat(jsonKaZhi.getString("11310"))) {
                    if (vbValue < Float.parseFloat(jsonKaZhi.getString("12410"))) {
                        stringBuffer.append("4");
                    } else {
                        stringBuffer.append("3");
                    }
                } else if (ppValue >= Float.parseFloat(jsonKaZhi.getString("11210"))) {
                    if (vbValue < Float.parseFloat(jsonKaZhi.getString("12410"))) {
                        stringBuffer.append("4");
                    } else if (vbValue < Float.parseFloat(jsonKaZhi.getString("12310"))) {
                        stringBuffer.append("3");
                    } else {
                        stringBuffer.append("2");
                    }
                } else if (ppValue >= Float.parseFloat(jsonKaZhi.getString("11110"))) {
                    if (vbValue < Float.parseFloat(jsonKaZhi.getString("12410"))) {
                        stringBuffer.append("4");
                    } else if (vbValue < Float.parseFloat(jsonKaZhi.getString("12310"))) {
                        stringBuffer.append("3");
                    } else if (vbValue < Float.parseFloat(jsonKaZhi.getString("12210"))) {
                        stringBuffer.append("2");
                    } else {
                        stringBuffer.append("1");
                    }
                } else {
                    stringBuffer.append("0");
                }
                // 下雪
            } else if (phValue == 302) {
                if (ppValue >= Float.parseFloat(jsonKaZhi.getString("77410"))) {
                    stringBuffer.append("4");
                } else if (ppValue >= Float.parseFloat(jsonKaZhi.getString("77310"))) {
                    if (vbValue < Float.parseFloat(jsonKaZhi.getString("72410"))) {
                        stringBuffer.append("4");
                    } else {
                        stringBuffer.append("3");
                    }
                } else if (ppValue >= Float.parseFloat(jsonKaZhi.getString("77210"))) {
                    if (vbValue < Float.parseFloat(jsonKaZhi.getString("72410"))) {
                        stringBuffer.append("4");
                    } else if (vbValue < Float.parseFloat(jsonKaZhi.getString("72310"))) {
                        stringBuffer.append("3");
                    } else {
                        stringBuffer.append("2");
                    }
                } else if (ppValue >= Float.parseFloat(jsonKaZhi.getString("77110"))) {
                    if (vbValue < Float.parseFloat(jsonKaZhi.getString("72410"))) {
                        stringBuffer.append("4");
                    } else if (vbValue < Float.parseFloat(jsonKaZhi.getString("72310"))) {
                        stringBuffer.append("3");
                    } else if (vbValue < Float.parseFloat(jsonKaZhi.getString("72210"))) {
                        stringBuffer.append("2");
                    } else {
                        stringBuffer.append("1");
                    }
                } else {
                    stringBuffer.append("0");
                }
                // 雨夹雪
            } else {
                stringBuffer.append("3");
            }
            stringBuffer.append(",");
        }
        return stringBuffer.substring(0, stringBuffer.length() - 1);
    }


    public String ATKaZhi(JSONObject jsonKaZhi, String qw) {
        String[] split = qw.split(",", -1);
        StringBuffer stringBuffer = new StringBuffer();
        for (String tmp : split) {
            float atValue = Float.parseFloat(tmp);
            if (atValue == -999) {
                stringBuffer.append("0");
            } else if (atValue >= Float.parseFloat(jsonKaZhi.getString("50210"))) {
                stringBuffer.append("2");
            } else if (atValue > Float.parseFloat(jsonKaZhi.getString("50110"))) {
                stringBuffer.append("1");
            } else {
                stringBuffer.append("0");
            }
            stringBuffer.append(",");
        }
        return stringBuffer.substring(0, stringBuffer.length() - 1);
    }

    public String RIKaZhi(JSONObject jsonKaZhi, String daolujiebing, String qiwen) {
        float riValue = Float.parseFloat(daolujiebing);
        float atValue = Float.parseFloat(qiwen);
        if (riValue == -999.9f || atValue == -999.9f) {
            return "-1";
        } else if (riValue == 0) {
            return "0";
        } else if (riValue == 1) {
            if (atValue <= Float.parseFloat(jsonKaZhi.getString("threshold14"))) {
                return "4";
            } else if (atValue <= Float.parseFloat(jsonKaZhi.getString("threshold13"))) {
                return "3";
            } else if (atValue <= Float.parseFloat(jsonKaZhi.getString("threshold12"))) {
                return "2";
            } else if (atValue <= Float.parseFloat(jsonKaZhi.getString("threshold11"))) {
                return "1";
            } else {
                return "0";
            }
        } else {
            return "0";
        }
    }

    private Map switchComposition(String fileNameValue, Map mapPointProp) {
        switch ((int) Float.parseFloat(fileNameValue)) {
            case 1:
                mapPointProp.put("Color", "#0066ff");// 蓝色
                break;
            case 2:
                mapPointProp.put("Color", "#ffff00");// 黄色
                break;
            case 3:
                mapPointProp.put("Color", "#ff7e00");// 橙色
                break;
            case 4:
                mapPointProp.put("Color", "#ff0000");// 红色
                break;
        }
        return mapPointProp;
    }

    /**
     * @param key
     * @param value
     * @param jsonPoint
     * @param jsonContent
     * @return
     * @description 获取路段线数据中风险告警需要的字段
     */
    public List getRiskLineInfo(String key, String value, JSONObject jsonPoint, JSONObject jsonContent) {
        List<String> rlist = new ArrayList<String>();
        JSONArray jarr = JSONObject.parseObject(value).getJSONArray("features");
        for (int i = 0; i < jarr.size(); i++) {
            JSONArray coordinates = jarr.getJSONObject(i).getJSONObject("geometry").getJSONArray("coordinates");
            // 根据经纬度取到桩点id
            String firstPoint = (String) jsonPoint.get(coordinates.getJSONArray(0).get(0) + "," + coordinates.getJSONArray(0).get(1));
            String lastPoint = (String) jsonPoint.get(coordinates.getJSONArray(coordinates.size() - 1).get(0) + "," + coordinates.getJSONArray(coordinates.size() - 1).get(1));
            // 根据桩点id取到其他信息
            JSONArray ss = jsonContent.getJSONArray(firstPoint);

            JSONObject properties = jarr.getJSONObject(i).getJSONObject("properties");
            String pst = properties.getString("pst");
            String Color = properties.getString("Color");
            // 高速编号 省份  县   区   桩号段1    桩号段2    相态  颜色 高速名称 key
            rlist.add(ss.getString(3) + "-" + ss.getString(0) + "-" + ss.getString(1) + "-" + ss.getString(2)
                    + "-" + firstPoint.split("_")[1] + "-" + lastPoint.split("_")[1] + "-" + pst + "-" + Color + "-" + ss.getString(4) + "-" + key);
        }
        return rlist;
    }

    /**
     * @param tupleQueue
     * @return
     * @descrition 路段数据所有预警信息点的处理
     */
    public String genPointDataToJson(List tupleQueue, JSONObject jsonLatLon) {
        JSONObject jsonMax = new JSONObject();
        Map mapMedium = new HashMap();
        Map mapMini = new HashMap();
        Map mapProp = new HashMap();
        JSONArray jsonArray = new JSONArray();
        String[] elementValues = new String[]{};
        try {
            Iterator<String> iterator = tupleQueue.iterator();
            // 要素类型
            while (iterator.hasNext()) {
                // value：要素值+相态值+桩点
                // 0_300_G1_710
                elementValues = iterator.next().split("_", 3);

                mapMini.put("type", "point");
                mapMini.put("coordinates", jsonLatLon.get(elementValues[2]));

                mapProp.put("ID", elementValues[2]);
                mapProp.put("value", elementValues[0]);
                // TODO 颜色这儿要进行switch匹配         获取对应的数值，是1=红色，2=橙色，3=黄色，4=蓝色
                mapProp = switchComposition(elementValues[0], mapProp);
                mapProp.put("pst", elementValues[1]);

                mapMedium.put("geometry", JSON.toJSON(mapMini));
                mapMedium.put("properties", JSON.toJSON(mapProp));
                jsonArray.add(JSON.toJSON(mapMedium));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jsonMax.put("features", jsonArray);
            if (jsonMax.toString().length() > 15) {
                return jsonMax.toString();
            } else {
                return null;
            }
        }

    }

    public String genMpgPointDataToJson(List tupleQueue, JSONObject jsonLatLon) {
        JSONObject jsonMax = new JSONObject();
        Map mapMedium = new HashMap();
        Map mapMini = new HashMap();
        Map mapProp = new HashMap();
        JSONArray jsonArray = new JSONArray();
        String[] elementValues = new String[]{};
        try {
            // 要素值_原始值_相态值_桩点
            Iterator<String> iterator = tupleQueue.iterator();
            // 要素类型
            mapMini.put("type", "point");
            while (iterator.hasNext()) {
                elementValues = iterator.next().split("_", 4);
                mapMini.put("coordinates", jsonLatLon.get(elementValues[3]));

                mapProp.put("Color", elementValues[0]);
                mapProp.put("value", elementValues[1]);
                mapProp.put("pst", elementValues[2]);
                mapProp.put("ID", elementValues[3]);

                mapMedium.put("geometry", JSON.toJSON(mapMini));
                mapMedium.put("properties", JSON.toJSON(mapProp));
                jsonArray.add(JSON.toJSON(mapMedium));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jsonMax.put("features", jsonArray);
            if (jsonMax.toString().length() > 15) {
                return jsonMax.toJSONString();
            } else {
                return null;
            }
        }
    }

    /**
     * @param tmpKeyLine
     * @param tupleIdQueue
     * @param tupleQueue
     * @param jsonLatLon
     * @return
     * @description
     */
    public String genZongHeDianData(String tmpKeyLine, List tupleIdQueue, List tupleQueue, JSONObject jsonLatLon) {
        JSONObject jsonMax = new JSONObject();
        Map mapMedium = new HashMap();
        Map mapMini = new HashMap();
        Map mapProp = new HashMap();
        JSONArray jsonArray = new JSONArray();
        try {
            for (int i = 0; i < tupleIdQueue.size(); i++) {

                mapMini.put("type", "point");
                mapMini.put("coordinates", jsonLatLon.get(tupleIdQueue.get(i).toString()));

                mapProp.put("ID", tupleIdQueue.get(i).toString());
                mapProp.put("value", tupleQueue.get(i).toString());
                // TODO 颜色这儿要进行switch匹配         获取对应的数值，是1=红色，2=橙色，3=黄色，4=蓝色
                mapProp = switchComposition(tupleQueue.get(i).toString(), mapProp);
                mapProp.put("pst", 333);

                mapMedium.put("geometry", JSON.toJSON(mapMini));
                mapMedium.put("properties", JSON.toJSON(mapProp));
                jsonArray.add(JSON.toJSON(mapMedium));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jsonMax.put("features", jsonArray);
            return jsonMax.toString();
        }
    }

    public String JSXTKaZhi(String jsxts) {
        float jsxtValue = Float.parseFloat(jsxts);
        if (jsxtValue < 0) {
            return "333";
        }
        return jsxts;
    }
}
